using System;
using System.Linq;
using DG.Tweening;
using UI;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Utilities;
using EventHandler = Utilities.EventHandler;

public class DragCard : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    public bool dragging = false;
    public Vector3 startPoint;
    public CardListController  cardSkillBox;
    public RectTransform cardSkillBoxRect;
    public Card card;
    

    

    public void Start()
    {
        cardSkillBox = GameObject.Find("CardSkillBox").GetComponent<CardListController>();
        cardSkillBoxRect= cardSkillBox.GetComponent<RectTransform>();
        card = GetComponent<Card>();
    }

    #region 拖拽
    public void OnBeginDrag(PointerEventData eventData)
    { //开始拖拽
        if (card.canSelect)
        {
            if (Input.mousePresent)
            {
                //eventData.button == PointerEventData.InputButton.Left
                if (!dragging)
                {
                    Debug.Log("按住鼠标左键");
                    startPoint = transform.position;
                    dragging = true;
                    cardSkillBox.isDrag = true;
                    EventHandler.CallPlaySoundEvent(SoundName.card1);
                    transform.DOScale(new Vector3(1.5f,1.5f,1.5f),0.5f).SetEase(Ease.OutQuart);
                }
            }
        }
    }
    public void OnDrag(PointerEventData eventData)
    {
        Vector3 mousePos = Input.mousePosition;
        transform.position = new Vector3(mousePos.x, mousePos.y, transform.position.z);
        cardSkillBox.currenPrewTime += Time.deltaTime;
        GetComponent<Image>().raycastTarget=false;
    }
    //取消拖拽，返回原来状态
    private void EndThisDrag()
    {
        dragging = false;
        cardSkillBox.isDrag = false;
        
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("松开鼠标左键");
        EndThisDrag();
        // 检测鼠标是否位于 CardListBox 内
        if (RectTransformUtility.RectangleContainsScreenPoint(
                cardSkillBoxRect, 
                eventData.position, 
                Camera.main))
        {
            #region 卡牌合并升级判断
            for (int i = 0; i < eventData.hovered.Count; i++)
            {
                if (eventData.hovered[i].TryGetComponent(out Card card1))
                {
                    if (card1.cardIndex != card.cardIndex&& card1.type==card.type)
                    {
                        Debug.Log("卡片对象异类");
                        if (card1.LevelUp())
                        {
                            Debug.Log("卡牌升级");
                            EventHandler.CallPlaySoundEvent(SoundName.cardLevelUp);
                            EventHandler.CallClearingCardsSlot(card.cardIndex);
                            Destroy(gameObject); //销毁卡片并激活升级
                            return;
                        }
                    }
                }
            }
            #endregion
            transform.position = startPoint; //返回原位置
            EventHandler.CallPlaySoundEvent(SoundName.card2);
            GetComponent<Image>().raycastTarget=true;
            transform.DOScale(new Vector3(1f,1f,1f),0.5f).SetEase(Ease.OutQuart);
        }
        else
        {
            Debug.Log("卡牌拖拽结束，鼠标位于 CardListBox 外！");
            if (card.canSelect)
            {
                GetComponent<Card>().ActivateCardEffect();
                Destroy(gameObject); //销毁卡片并激活效果
                return;
            }
        }
    }

    #endregion
    
    //拖拽中每帧更新位置
    private void Update()
    {
        // if (dragging)
        // {
        //     Vector3 mousePos = Input.mousePosition;
        //     transform.position = new Vector3(mousePos.x, mousePos.y, transform.position.z);
        //     cardSkillBox.currenPrewTime += Time.deltaTime;
        // }
    }
    
}